package org.geektimes.projects.user.web.listener;

import org.geektimes.projects.user.web.filter.CharsetEncodingFilter;
import org.geektimes.web.WebApplicationInitializer;
import org.geektimes.web.mvc.FrontControllerServlet;
import org.jolokia.http.AgentServlet;

import javax.servlet.*;
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
import java.util.*;

import static javax.servlet.DispatcherType.*;

/**
 * 替代web.xml
 * @author tom
 * @since 2021/3/24 23:51
 */
public class ServletConfigInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //注册DefaultServlet
//        String defaultServlet = "default";
//        String defaultServletClass = "org.apache.catalina.servlets.DefaultServlet";
//        ServletRegistration.Dynamic defaultServletRegistration = servletContext.addServlet(defaultServlet, defaultServletClass);
//        defaultServletRegistration.setInitParameter("debug", "0");
//        defaultServletRegistration.setInitParameter("listings", "false");
//        defaultServletRegistration.setLoadOnStartup(1);
//        defaultServletRegistration.addMapping("*.css", "*.js");

        // 注册{@link FrontControllerServlet}
        String frontControllerServlet = "FrontControllerServlet";
        ServletRegistration.Dynamic frontControllerServletRegistration = servletContext.addServlet(frontControllerServlet, new FrontControllerServlet());
        frontControllerServletRegistration.setLoadOnStartup(1);
        frontControllerServletRegistration.addMapping("/");

        //注册AgentServlet
        String agentServlet = "AgentServlet";
        ServletRegistration.Dynamic agentServletRegistration = servletContext.addServlet(agentServlet, new AgentServlet());
        agentServletRegistration.setLoadOnStartup(2);
        agentServletRegistration.addMapping("/jolokia/*");

        //注册CharsetEncodingFilter
        String charsetEncodingFilterName = "charsetEncodingFilter";
        FilterRegistration.Dynamic charsetEncodingFilter = servletContext.addFilter(charsetEncodingFilterName, CharsetEncodingFilter.class);
        charsetEncodingFilter.setInitParameter("encoding", "UTF-8");
        charsetEncodingFilter.addMappingForUrlPatterns(EnumSet.of(FORWARD, REQUEST, INCLUDE, ERROR),true, "/*");

        JspPropertyGroupDescriptor jspGroupDescriptor = new MyJspPropertyGroupDescriptor() {
            @Override
            public Collection<String> getUrlPatterns() {
                return Collections.singleton("*.jsp");
            }
            @Override
            public Collection<String> getIncludePreludes() {
                return Collections.singleton("/WEB-INF/jsp/prelude/header.jspf");
            }
            @Override
            public Collection<String> getIncludeCodas() {
                return Collections.singleton("/WEB-INF/jsp/coda/footer.jspf");
            }
        };
        JspPropertyGroupDescriptor jspfGroupDescriptor = new MyJspPropertyGroupDescriptor() {
            @Override
            public Collection<String> getUrlPatterns() {
                return Collections.singleton("*.jspf");
            }
            @Override
            public Collection<String> getIncludePreludes() {
                List<String> preludes = new ArrayList<>();
                preludes.add("/WEB-INF/jsp/prelude/include-taglibs.jspf");
                preludes.add("/WEB-INF/jsp/prelude/variables.jspf");
                return preludes;
            }
        };
//        servletContext.getJspConfigDescriptor().getJspPropertyGroups().add(jspGroupDescriptor);
//        servletContext.getJspConfigDescriptor().getJspPropertyGroups().add(jspfGroupDescriptor);
    }

    static class MyJspPropertyGroupDescriptor implements JspPropertyGroupDescriptor{

        @Override
        public Collection<String> getUrlPatterns() {
            return null;
        }

        @Override
        public String getElIgnored() {
            return null;
        }

        @Override
        public String getPageEncoding() {
            return "UTF-8";
        }

        @Override
        public String getScriptingInvalid() {
            return null;
        }

        @Override
        public String getIsXml() {
            return null;
        }

        @Override
        public Collection<String> getIncludePreludes() {
            return null;
        }

        @Override
        public Collection<String> getIncludeCodas() {
            return null;
        }

        @Override
        public String getDeferredSyntaxAllowedAsLiteral() {
            return null;
        }

        @Override
        public String getTrimDirectiveWhitespaces() {
            return "true";
        }

        @Override
        public String getDefaultContentType() {
            return null;
        }

        @Override
        public String getBuffer() {
            return null;
        }

        @Override
        public String getErrorOnUndeclaredNamespace() {
            return null;
        }
    }
}
